/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isvp.sax;

import isvp.constants.ISVPConstants;
import isvp.sax.handlers.IsvXMLHandler;
import isvp.utilities.XMLUtils;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author jjophin
 */
public class Saxer {
    
    private static SAXParserFactory factory = null;
    private static IsvXMLHandler isvHandler = new IsvXMLHandler();
    private static SAXParser saxParser;
    
    private static SAXHandler handler;
    
    static {
        factory = SAXParserFactory.newInstance();
        try {
            saxParser = factory.newSAXParser();
        } catch (ParserConfigurationException ex) {
        } catch (SAXException ex) {
        }
    }
    
    private static void setHandler(String handlerType) {
        if(handlerType.equals(ISVPConstants.ISV_XML_FILE)) {
            handler = isvHandler;
        }
        handler.initialize();
    }
    
    public static Document parse(InputStream iStream, String handlerType) throws SAXException, IOException {
        setHandler(handlerType);
        saxParser.parse(iStream, handler);
        String xmlString = handler.getResult();
        Document doc = XMLUtils.getXMLDocument(xmlString);
        return doc;
    }    
}
